package com.shanjupay.common.config.redis;


import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 默认采用的是String的序列化策略，
 * 保存的key和value都是采用此策略序列化保存的。
 * @author dudu
 */
@Service
public class StringRedisServiceImpl implements RedisService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void hmSet(String key, String hashKey, String value) {
        stringRedisTemplate.opsForHash().put(key,hashKey,value);
    }

    @Override
    public void hmSet(String key, Map<String, String> map) {
        stringRedisTemplate.opsForHash().putAll(key,map);
    }

    @Override
    public String hmGet(String key, String hashKey) {
        return (String) stringRedisTemplate.opsForHash().get(key,hashKey);
    }

    @Override
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean set(String key, String value) {
        boolean result = false;
        try{
            stringRedisTemplate.opsForValue().set(key, value);
            result = true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean set(String key, String value, Long expireTime) {
        boolean result = false;
        try{
            stringRedisTemplate.opsForValue().set(key, value);
            stringRedisTemplate.expire(key,expireTime, TimeUnit.SECONDS);
            result = true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void remove(String key) {
        stringRedisTemplate.delete(key);
    }

    @Override
    public boolean exists(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    @Override
    public boolean expire(String key, Long expireTime) {
        boolean result = false;
        try {
            stringRedisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Set<String> keys(String key) {
        return stringRedisTemplate.keys(key);
    }

    @Override
    public String incr(String key) {
        long count =  stringRedisTemplate.opsForValue().increment(key,1);
        return count +"";
    }

    @Override
    public String incrBy(String key, int range) {
        long count =  stringRedisTemplate.opsForValue().increment(key,range);
        return count +"";
    }

    @Override
    public String decr(String key) {
        long count =  stringRedisTemplate.opsForValue().increment(key,-1);
        return count +"";
    }

    @Override
    public long sadd(String key, String... value) {
        return stringRedisTemplate.opsForSet().add(key,value);
    }

    @Override
    public void expire(String key, long expireTime, TimeUnit timeUnit) {
        stringRedisTemplate.expire(key, expireTime, timeUnit);
    }

}
